package com.lizhao.win007com.info.webapi.service.impl;

import com.lizhao.win007com.commons.pojo.info.model.*;
import com.lizhao.win007com.commons.pojo.match.model.Match;
import com.lizhao.win007com.info.service.IInfoService;
import com.lizhao.win007com.info.webapi.mapper.InfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@DubboService
public class InfoServiceImpl implements IInfoService {
    @Autowired
    private InfoMapper infoMapper;

    //获取公司信息
    @Override
    public List<CompanyInfo> getAllCompanyInfo() {
        return infoMapper.findAllCompanyInfo();
    }

    @Override
    public List<RateInfo> getRateByLeagueIdAndRelationAndRateTag(Integer leagueid, String relation, String ratetag) {
        return infoMapper.findRateByLeagueIdAndRelationAndRateTag(leagueid, relation, ratetag);
    }

    // 通过pageid获取平赔简易版数据
    @Override
    public MonitorEasyInfo getEasyInfoByPageId(Integer pageid) {
        return infoMapper.findEasyInfoByPageId(pageid);
    }

    @Override
    public List<MonitorEasyInfo> getMonitorEasyInfo() {
        List<MonitorEasyInfo> monitorEasyInfoList = infoMapper.findEasyMonitor();
        for (MonitorEasyInfo monitorEasyInfo : monitorEasyInfoList){
            // 通过pageid获取开盘赔型总数
            MonitorPInfo pInfo60 = infoMapper.findEasyPByPageIdAndTtype(monitorEasyInfo.getPageid(), 60);
            MonitorPInfo pInfo30 = infoMapper.findEasyPByPageIdAndTtype(monitorEasyInfo.getPageid(), 30);
            MonitorPInfo pInfo2 = infoMapper.findEasyPByPageIdAndTtype(monitorEasyInfo.getPageid(), 2);

            if (pInfo60 != null){
                monitorEasyInfo.setTotal60(pInfo60.getTotal());
            }

            if (pInfo30 != null){
                monitorEasyInfo.setTotal30(pInfo30.getTotal());
            }

            if (pInfo2 != null){
                monitorEasyInfo.setTotal2(pInfo2.getTotal());
            }

        }
        return monitorEasyInfoList;
    }

    @Override
    public List<MonitorAvgInfo> getMonitorInfo() {
        List<MonitorAvgInfo> monitorInfoList = infoMapper.findMonitor();
        for (MonitorAvgInfo monitorInfo : monitorInfoList){
            String[] date = monitorInfo.getMatchdatetime().split(" ");
            monitorInfo.setMatchdate(date[0]);
            monitorInfo.setMatchtime(date[1].substring(0, 5));
        }
        return monitorInfoList;
    }

    //主流胜率数据
    @Override
    public List<RateInfo> getFullRateInfoByLeagueIdAndrelation(Integer leagueid, String relation) {
        String table = "leaguematch-rate-abs-current-full";
        String ratetag = null;

        switch (relation){
            case "C>B>A13":
                ratetag = "rate31";
                break;
            case "C>A>B31":
                ratetag = "rate31";
                break;
            case "A>B>C10":
                ratetag = "rate10";
                break;
            case "A>C>B01":
                ratetag = "rate10";
                break;
            default:
        }

        Map<Integer, LeagueInfo> leagueInfoMap = new HashMap<>();
        Map<Integer, CompanyInfo> companyInfoMap = new HashMap<>();

        //获取联赛配置信息
        List<LeagueInfo> tmpLeagueInfo = getAllLeagueInfo();
        for (LeagueInfo leagueInfo : tmpLeagueInfo){
            leagueInfoMap.put(leagueInfo.getLeagueid(), leagueInfo);
        }

        //获取公司信息
        List<CompanyInfo> tmpCompanyInfo = getAllCompanyInfo();
        for (CompanyInfo companyInfo : tmpCompanyInfo){
            companyInfoMap.put(companyInfo.getCompanyid(), companyInfo);
        }

        //主流胜率数据
        List<RateInfo> fullRateInfo = infoMapper.findFullRateInfo(table, leagueid, ratetag);
        for (RateInfo rateInfo : fullRateInfo){
            rateInfo.setLeaguetype(leagueInfoMap.get(rateInfo.getLeagueid()).getLeaguetype());
            rateInfo.setCompanyname1(companyInfoMap.get(rateInfo.getCompanyid1()).getCompanyname());
            rateInfo.setCompanyname2(companyInfoMap.get(rateInfo.getCompanyid2()).getCompanyname());
        }

        return fullRateInfo;
    }

    //获取联赛配置信息
    @Override
    public List<LeagueInfo> getAllLeagueInfo() {
        return infoMapper.findAllLeagueInfo();
    }

    //获取开放的联赛赛事类型
    @Override
    public List<LeagueInfo> getOpenLeagueInfo(){
        return infoMapper.findOpenLeagueInfo();
    }

    //获取开放的杯赛赛事类型
    @Override
    public List<CupInfo> getOpenCupInfo(){
        return infoMapper.findOpenCupInfo();
    }

    @Override
    public List<CupInfo> getAllCupInfo() {
        return infoMapper.findAllCupInfo();
    }

    //获取配置模块信息
    @Override
    public List<TypeInfo> getAllTypeInfo() {
        return infoMapper.findAllTypeInfo();
    }

    //添加盘口信息到odds表
    @Override
    public void addOdds(String type,String season,List<Match> oddsList) {
        System.out.println(oddsList);
        //让盘口信息批量进入盘口品种表
        for (Match match : oddsList){
            Odds odds = new Odds();
            Integer id = null;

            odds.setId(id);
            odds.setType(type);
            odds.setSeason(season);
            odds.setOdds(match.getOdds());
            infoMapper.insertOdds(odds);
        }
    }

    //通过leagueid获取联赛信息
    @Override
    public LeagueInfo getLeagueInfoByLeagueid(Integer leagueid) {
        return infoMapper.findLeagueInfoByLeagueid(leagueid);
    }

    //清空表
    @Override
    public void truncateTable(String table) {
        infoMapper.truncateByTable(table);
    }

    //修改入库配置
    @Override
    public Integer updateByTableAndIdAndValue(String table,Integer id, String field, String value) {
        return infoMapper.updateInfo(table,id,field,value);
    }

    //修改配置路径
    @Override
    public Integer updateConfigInfo(String path,Integer sleep) {
        return infoMapper.updateConfig(path,sleep);
    }

    //获取配置python生成文件的信息
    @Override
    public List<ConfigInfo> getConfigInfo() {
        return infoMapper.findConfigInfo();
    }
}
